package com.abc.robot01.activity.compose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.GenericShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import com.abc.robot01.R

class ClipDemo : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val scrollState = rememberScrollState()
            Column(
                modifier = Modifier
                    .verticalScroll(scrollState)
                    .fillMaxSize()
            ) {
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
                ClippedBox()
                Spacer(modifier = Modifier.height(2.dp))
                CustomClip()
                Spacer(modifier = Modifier.height(2.dp))
            }
        }
    }
}

@Composable
fun ClippedBox() {
    Box(
        modifier = Modifier.size(200.dp).background(Color.Blue).clip(CircleShape)
    ) {
        Image(
            painter = painterResource(id = R.drawable.a1),
            contentDescription = "Image",
            modifier = Modifier.fillMaxSize()
        )
    }
}

@Composable
fun CustomClip() {
    val customShape = GenericShape { size, _ ->
        // 自定义形状: 画一个三角形
        moveTo(size.width / 2, 0f)
        lineTo(size.width, size.height)
        lineTo(0f, size.height)
        close()
    }

    Box(
        modifier = Modifier
            .size(200.dp)
            .clip(customShape) // 使用自定义形状裁剪
            .background(Color.Red)
    ) {
//        Image(
//            painter = painterResource(id = R.drawable.aa),
//            contentDescription = "Custom Clipped Image",
//            modifier = Modifier.fillMaxSize()
//        )
    }
}
